﻿var strUrl = document.location.href.toLowerCase();

$(function () {
    $(window).bind("load resize", function () {
        var topHigh = $("header").outerHeight();
        var botHigh = $("footer").outerHeight();
        var height = ((this.window.innerHeight > 0) ? this.window.innerHeight : this.screen.height) - 1;
        height = height - topHigh - botHigh + 2;
        if (height < 1) height = 1;
        if (height > 0) {
            $("#mainWarp").css("min-height", (height) + "px");
        }
    });

});

function amAlert(content, title) {
    if (title) {
        $('#my-alert').find(".am-modal-hd").html(title);
    }
    $('#my-alert').find(".am-modal-bd").html(content);
    var $alert = $('#my-alert').modal();
}

function amAlertFun(content, fun) {
    $('#my-alert').find(".am-modal-bd").html(content);
    var $alert = $('#my-alert').modal();

    $('#my-alert').on('closed.modal.amui', fun);
}

function amLoading(title) {
    if (title) {
        $('#my-modal-loading').find(".am-modal-hd").html(title);
    }

    var $loading = $('#my-modal-loading').modal();
}

function amLoadingHide() {
    $('#my-modal-loading').modal('close');
}

//url获取参数
function GetQueryString(name) {
    var result = location.search.match(new RegExp("[\?\&]" + name + "=([^\&]+)", "i"));
    if (result == null || result.length < 1) {
        return "";
    }
    return result[1];
}


//写cookies 
function setCookie(name, value) {
    var Days = 30;
    var exp = new Date();
    exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
    document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
}

//读取cookies 
function getCookie(name) {
    var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
    if (arr = document.cookie.match(reg))
        return unescape(arr[2]);
    else
        return "";
}

//删除cookies 
function delCookie(name) {
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval = getCookie(name);
    if (cval != null)
        document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
}


function login() {
    if (!$('#fmLogin').data('amui.validator').isFormValid()) {
        return false;
    }

    var data = $("#fmLogin").serialize();
    $.ajax({
        type: "POST",
        url: "../handler/RegisterHandler.ashx",
        dataType: "json",
        data: data,
        success: function (result) {
            switch (result["state"]) {
                case "success":
                    commonJs.showInfoTips("登录成功！");
                    setTimeout(function () { window.location.href = location.href; }, 500);
                    break;
                case "norecord":
                    commonJs.showInfoTips("用户名或密码不对！");
                    break;
                case "error":
                    alert(result["msg"]);
                    break;
                default:
                    break;
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {  //#3这个error函数调试时非常有用，如果解析不正确，将会弹出错误框
            alert(XMLHttpRequest.status);
            alert(XMLHttpRequest.readyState);
            alert(textStatus); // paser error;
        },
        beforeSend: function () {
            $("#btnLogin").attr("disabled", "disabled");
        },
        complete: function () {
            $("#btnLogin").removeAttr("disabled");
        }
    });
}

function logout() {
    $.ajax({
        type: "POST",
        url: "../handler/RegisterHandler.ashx",
        dataType: "json",
        data: "act=logout",
        success: function (result) {
            switch (result["state"]) {
                case "success":
                    commonJs.showInfoTips("已注销！");
                    setTimeout(function () { window.location.href = location.href; }, 500);
                    break;
                case "error":
                    alert(result["msg"]);
                    break;
                default:
                    break;
            }
        }
    });
}

function ConvertTime(date) {
    var da = new Date(parseInt(date.replace("/Date(", "").replace(")/", "").split("+")[0]));
    return da.getFullYear() + "-" + (da.getMonth() + 1) + "-" + da.getDate() + " " + da.getHours() + ":" + da.getSeconds() + ":" + da.getMinutes();
}

function ConvertTimeNoHour(date) {
    if (date == undefined || date == "") {
        return "";
    }
    else {
        var da = new Date(parseInt(date.replace("/Date(", "").replace(")/", "").split("+")[0]));
        return da.getFullYear() + "-" + (da.getMonth() + 1) + "-" + da.getDate();
    }

}

var scrollVal = $(document).scrollTop();
var token = localStorage.getItem("token");

$(function () {
    //判断用户是否登录
    token = localStorage.getItem("token");
    if (token == null || token == "" || token == undefined)
    {
        $("#mpNoLogin").show();
        $("#mpLogin").hide();
    }
    else
    {
        $("#mpLogin").show();
        $("#mpNoLogin").hide();
    }
    
    $(window).scroll(function () {
        var nowVal = $(document).scrollTop();
        if (nowVal < scrollVal || nowVal < 800) {
            if ($(".web-nav").is(':hidden')) {
                $(".web-nav").show();
            }            
        }
        else {
            $(".web-nav").hide();            
        }
        scrollVal = nowVal;        
        //置顶显示
        if (nowVal > 800) {
            if ($(".web-totop").is(':hidden'))
            {
                $(".web-totop").show();
            }
            
        } else {
            $(".web-totop").hide();
        }
    })
})

//退出
function logout()
{
    token = "";
    localStorage.removeItem("token");
    localStorage.removeItem("nickname");
    $("#mpNoLogin").show();
    $("#mpLogin").hide();
}

//评论
function commentJoke(obj,type) {
    var nickname = $("#txtNickname").val();
    var comment = $("#txtCommnet").val();
    if (comment == "") {
        amAlert("请填写评论内容！");
        return false;
    }
    var id = GetQueryString("id");
    $(obj).attr("disabled", "disabled");
    $(obj).html("<i class=\"am-icon-spinner am-icon-pulse\"></i>&nbsp;提交中</a>");

    if (nickname == "") {
        nickname = "匿名用户";
    }
    
    $.ajax({
        type: "POST",
        url: "../handler/joke.ashx",
        dataType: "json",
        data: "act=comment&type=" + type + "&jokeid=" + id + "&token=" + token + "&nickname=" + nickname + "&comment=" + comment,
        success: function (result) {
            switch (result["state"]) {
                case "success":
                    var nowDate = new Date;
                    nickname = nickname + " [" + nowDate.getFullYear() + "-" + (nowDate.getMonth() + 1) + "-" + nowDate.getDate() + "]"
                    var floor = $(".joke-commentlist li").length + 1;
                    var strComment = " <li class=\"am-g\">";
                    strComment += "<div class=\"joke-commentlist-content\">";
                    strComment += "<p class=\"joke-commentlist-user am-margin-bottom-xs\">" + nickname + "</p>";
                    strComment += "<div>" + comment + "</div>";
                    strComment += "</div>";
                    strComment += "<span class=\"joke-commentlist-index\">" + floor + "</span>";
                    strComment += "</li>"
                    $(".joke-commentlist").append(strComment);
                    break;
                case "error":
                    alert(result["msg"]);
                    break;
                default:
                    break;
            }
        },
        beforeSend: function () {
            $(obj).attr("disabled", "disabled");
        },
        complete: function () {
            $(obj).html("提  交").removeAttr("disabled");
        }
    });

    $("#txtNickname").val("");
    $("#txtCommnet").val("");
}

